class Employee < ActiveRecord::Base
  has_many :purchase_order
  belongs_to :department
  belongs_to :role
  belongs_to :company

  scope :registered, ->(time_ago) { where('created_at > ?', time_ago)}

  #scope :name_like, lambda { |name| { :conditions => ['name like ? OR number like ? OR ab like ?', '%'+name+'%','%'+name+'%','%'+name+'%'],:include=>[:department,:role],:order=>"id,number" }}

  scope :name_like, lambda { |name, for_cbb, company_id|
    if for_cbb == 'false'
      if company_id.nil?
        where('name like ? OR number like ? OR ab like ?', '%'+name+'%', '%'+name+'%', '%'+name+'%').includes(:company,:department, :role)
      else
        where('(name like ? OR number like ? OR ab like ?) AND company_id = ?', '%'+name+'%', '%'+name+'%', '%'+name+'%', company_id).includes(:company,:department, :role)
      end

    else
      if company_id.nil?
        where('name like ? OR number like ? OR ab like ?', '%'+name+'%', '%'+name+'%', '%'+name+'%').select([:id, :name, :number])
      else
        where('(name like ? OR number like ? OR ab like ?) AND company_id = ?', '%'+name+'%', '%'+name+'%', '%'+name+'%',company_id).select([:id, :name, :number])
      end
    end
  }

  scope :paginate, ->(start,limit) { offset(start.to_i).limit(limit.to_i).order("department_id DESC")}

end
